
class WoW_API
  @@base_url = "http://us.battle.net/api/wow/"
  @@toons = Hash.new
  def initialize
  end

  def self.time_elapsed(start_time)
    current_time = Time.now
    difference = current_time - start_time
    return (difference * 1000).to_i
  end

  def self.get_realms(realm_names = nil)
    realm_status_url = "realm/status"
    if realm_names != nil
      if realm_names.kind_of? String
        realm_status_url = "#{realm_status_url}?realms=#{realm_names}"
      elsif realm_names.kind_of? Array
        realm_status_url = "#{realm_status_url}?realms=#{realm_names.join(',')}"
      else
        raise("Unsupported realm_names type: #{realm_names.class} sent to WoW_API.get_realms")
      end
    end
    url = "#{@@base_url}#{realm_status_url}"
    resp = Net::HTTP.get_response(URI.parse(url))
    data = resp.body
    result = JSON(data)
    realms = Hash.new
    result["realms"].each { |realm|
      realms[realm["name"]] = Realm.new(realm["name"], realm["type"], realm["battlegroup"], realm["queue"], realm["status"], realm["population"], realm["slug"])
    }
    realms
  end

  def self.get_character(name, realm, fields = nil, get_fresh = false)
    #standardize string format
    name.downcase!
    realm.downcase!
    fields.downcase! if fields.kind_of? String

    #acknowledge request received
    start_time = Time.now
    puts "#{start_time}: Received request for #{name} @ #{realm}"

    #check to see if we can just pull from existing database to avoid throttling
    if !get_fresh
      if @@toons.has_key?(realm) and @@toons[realm].has_key?(name)
        puts "#{Time.now}: Request completed - Found in database, requested fields may not be included. Time elapsed: #{time_elapsed(start_time)}"
        return @@toons[realm][name]
      end
    end

    #Format request and get new data through api
    character_status_url = "character/"
    url = "#{@@base_url}#{character_status_url}#{realm}/#{name}"
    if fields != nil
      if fields.kind_of? String
        url = "#{url}?fields=#{fields}"
      elsif fields.kind_of? Array
        url = "#{url}?fields=#{fields.join(',')}"
      else
        raise("Unsupported fields type: #{fields.class} sent to WoW_API.get_realms")
      end
    end
    resp = Net::HTTP.get_response(URI.parse(url))
    data = resp.body
    character = JSON(data)
    toon = Character.new(character["realm"], character["name"], character["level"], character["lastModified"], character["thumbnail"], character["race"], character["achievementPoints"], character["gender"], character["class"])
    if character["titles"] != nil
      character["titles"].each { |title|
        toon.add_title Title.new(title["id"], title["name"], if title.has_key?("selected") then title["selected"] else false end)
      }
    end
    #need to implement long term storage for guilds before using as guild information is "expensive"
    #if character["guild"] != nil
    #    emblem = Emblem.new(character["guild"]["emblem"]["icon"], character["guild"]["emblem"]["iconColor"], character["guild"]["emblem"]["border"], character["guild"]["emblem"]["borderColor"], character["guild"]["emblem"]["backgroundColor"]).inspect
    #    toon.add_guild Guild.new(character["guild"]["name"], character["guild"]["realm"], character["guild"]["level"], character["guild"]["members"], character["guild"]["achievementPoints"], emblem).inspect
    #end

    #Add data to toons database
    if !@@toons.has_key?(realm)
      @@toons[realm] = Hash.new
    end
    @@toons[realm][name] = toon
    puts "#{Time.now}: Request completed - New data from api obtained. Time elapsed: #{time_elapsed(start_time)}"
    toon
  end
end